<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>图书管理</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        function page(n, s) {
            $("#pageNo").val(n);
            $("#pageSize").val(s);
            $("#searchForm").submit();
            return false;
        }
        var index = 1;
        function addBatchDetail() {
            var html = "<tr id='tr_" + index + "'><td>" + index + "</td>" +
                    "<td><input name='isbn' onchange='searchBook(this)'><input name='bkId' type='hidden'></td>" +
                    "<td></td><td></td><td></td><td></td>" +
                    "<td><input name='price' size='10' width='10' readonly style='width: 100px;background-color: #eeeeee;'></td>" +
                    "<td><input name='mxCnt' size='10' width='10' style='width: 50px;'></td>" +
                    "<td><a href='javascript:deleteBookInfo(\"tr_" + index + "\")'>删除</a></td></tr>";
            $("#details").append(html);
            index++;
        }
        function searchBook(obj) {
            var isbn = $(obj).val();
            $.ajax({
                type: "POST",
                url: "${ctx}/book/manage/getByIsbn",
                data: {isbn: isbn},
                dataType: "json",
                success: function (data) {
                    var id = data.id;
                    var bookname = data.bookname;
                    var author = data.author;
                    var classcode = data.classcode;
                    var publisher = data.publisher;
                    var pubdate = data.pubdate;
                    var price = data.price;
                    $(obj).siblings().val(id);
                    $(obj).parents("td").siblings().each(function (i) {
                        if (i == 1) {
                            $(this).html(bookname);
                        }
                        else if (i == 2) {
                            $(this).html(classcode);
                        }
                        else if (i == 3) {
                            $(this).html(publisher);
                        }
                        else if (i == 4) {
                            $(this).html(pubdate);
                        }
                        else if (i == 5) {
                            $(this).find("input").val(price);
                        }
                        /*else if (i == 6) {
                            $(this).find("input").val(price);
                        }*/
                    });
                }
            });
        }
        function deleteBookInfo(id) {
            $("#" + id).remove();
        }
        function checkBkId() {
            $("[name='bkId']").each(function () {
                if ($(this) == undefined || $(this).val() == "") {
                    alertx("要保存的【图书信息】不全！");
                    return false;
                }
            });
            return true;
        }
        var r = /^[0-9]*[1-9][0-9]*$/;
        function checkMxCnt() {
            $("[name='mxCnt']").each(function () {
                if ($(this) == undefined || $(this).val() == "" || !r.test($(this).val())) {
                    alertx("输入的【采购量】信息错误！");
                    return false;
                }
            });
            return true;
        }
        function checkPrice() {
            $("[name='price']").each(function () {
                if ($(this) == undefined || $(this).val() == "" || !isFinite($(this).val())) {
                    alertx("输入的【价格】信息错误！");
                    return false;
                }
            });
            return true;
        }
//        function checkPlanDate() {
//            var flag = true;
//            if ($("[name='planDate']").val() == "") {
//                alertx("请选择采购日期！");
//                flag = false;
//            }
//            return flag;
//        }
        function subbmitPurchase() {
            if (checkBkId() && checkPrice() && checkMxCnt()) {
                $.ajax({
                    type: "POST",
                    url: "${ctx}/book/purchase/saveplan",
                    data: {param: $("#purchaseForm").serialize()},
                    dataType: "json",
                    success: function (data) {
                        if (data.code == "0001") {

                            top.$.jBox.info(data.msg, '提示', {
                                closed: function () {
                                    window.location.href = "${ctx}/book/purchase/list?id=" + data.data.batchId;
                                }
                            });
                            top.$('.jbox-body .jbox-icon').css('top', '55px');

                        } else {
                            alertx("保存信息失败");
                        }
                    }
                });
            }
        }
    </script>
</head>
<body>
<ul class="nav nav-tabs">
    <li><a href="${ctx}/book/purchase/list">采购批次</a></li>
    <li class="active"><a href="${ctx}/book/purchase/form">新增批次</a></li>
</ul>
<br/>
<form:form id="purchaseForm" modelAttribute="bkPurchaseBatch" action="${ctx}/book/purchase/" method="post"
           class="form-horizontal">
    <div class="control-group">
        <label class="control-label">批次编号:</label>

        <div class="controls">
            <form:input path="batchNumber" htmlEscape="false" maxlength="20" class="input-medium"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">计划时间:</label>

        <div class="controls">
            <form:input path="planDate" htmlEscape="false" readonly="true" maxlength="20"
                        class="input-medium Wdate"
                        pattern="yyyy-MM-dd HH:mm:ss"
                        onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">备注：</label>

        <div class="controls">
            <form:textarea path="remarks" htmlEscape="false" maxlength="30" class="input-medium"/>
        </div>
    </div>
    <input class="btn btn-primary" type="button" onclick="addBatchDetail()" value="新增明细"/>&nbsp;&nbsp;
    <table id="contentTable" class="table table-striped table-bordered table-condensed">
        <thead>
        <tr>
            <th width="20">序号</th>
            <th>ISBN</th>
            <th>书名</th>
            <th>分类号</th>
            <th>出版社</th>
            <th>出版日期</th>
            <th>价格</th>
            <th>采购量</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="details">
        </tbody>
    </table>
    <div class="form-actions">
        <input id="btnSubmit" class="btn btn-primary" style="margin-left: 30%;" type="button"
               onclick="subbmitPurchase()" value="保存批次"/>
    </div>
</form:form>
</body>
</html>